package me.lc.superDown.layer;

import me.lc.superDown.R;
import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.sprite.RunGuy;
import me.lc.superDown.util.GameUtil;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.CallFunc;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYPoint;

public class LoadingLayer extends ColorLayer {
	public LoadingLayer() {
		super(WYColor4B.make(255, 255, 255, 255));

		// 显示loading动画
		Animation animation = new Animation(0);
		animation.addFrame(0.1f, RunGuy.frameAt(1), RunGuy.frameAt(2),
				RunGuy.frameAt(3), RunGuy.frameAt(4), RunGuy.frameAt(5),
				RunGuy.frameAt(6));
		animation.autoRelease();
		Animate animate = Animate.make(animation);
		animate.autoRelease();

		Texture2D tex = Texture2D.makePNG(R.drawable.man_face_right);

		Sprite sprite = Sprite.make(tex, RunGuy.frameAt(1));
		addChild(sprite);
		sprite.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2);
		sprite.autoRelease();

		sprite.runAction((Action) RepeatForever.make(animate).autoRelease());
		sprite.runAction((CallFunc) CallFunc.make(this, "loadResource")
				.autoRelease());

		Sprite logo = Sprite.make(Texture2D.makeJPG(R.drawable.wiyun_logo));
		addChild(logo);
		logo.setAnchor(1, 0);
		logo.setPosition(WYPoint.make(GameSystem.Screen_Width, 0));
		logo.autoRelease();
	}

	/**
	 * 加载系统资源
	 */
	public void loadResource() {
		new Thread() {
			public void run() {
				GameSystem.loadAllResource();
				// ADHelper.initAD();
				gotoGameMenu();
			}
		}.start();
	}

	/**
	 * 切换到游戏菜单
	 */
	public void gotoGameMenu() {
		GameUtil.switchSceneNormal(GameScene.make());
		GameManager.instance.startMainMenu(GameScene.make().layer);
	}
}
